package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.LagouUserDao;
import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.pojo.LagouUser;
import com.lagou.edu.service.AutoCodeServiceFeignClient;
import com.lagou.edu.service.LagouUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.util.UUID;

/**
 * @Author: Tao Zheng
 * @classNmae: LagouUserServiceImpl
 * @packageName: com.lagou.edu.service.impl
 * @description:
 * @DATE: 2021/9/7 19:11
 */
@Service
public class LagouUserServiceImpl implements LagouUserService {

    @Autowired
    private LagouUserDao userDao;

    @Autowired
    private LagouTokenDao tokenDao;

    @Autowired
    private AutoCodeServiceFeignClient codeServiceFeignClient;

    @Override
    public LagouToken register(String email, String password, String code) {

        // 1. 判断是否已注册
        boolean isRegistered = isRegistered(email);
        // 2. 判断验证码是否正确
        //1:正确，2:错误，0:超时
        int validateRes = codeServiceFeignClient.validateCode(email, code);

        if (!isRegistered && validateRes == 1){

            // 3. 注册用户
            Date createTime = new Date(System.currentTimeMillis());

            LagouUser user = new LagouUser();
            user.setEmail(email);
            user.setName("test");
            user.setPassword(password);
            user.setCreateTime(createTime);
            user.setLoginTime(createTime);   // 记录登录时间
            userDao.save(user);

            // 4. 生成token并存入数据库
            String tokenText = email + password;
            UUID uuid = UUID.nameUUIDFromBytes(tokenText.getBytes());

            LagouToken token = new LagouToken();
            token.setEmail(email);
            token.setToken(uuid.toString());
            return tokenDao.save(token);
        }
        return null;
    }

    @Override
    public LagouToken login(String email, String password) {

        LagouUser user = userDao.findFirstByEmail(email);
        if (user != null && user.getPassword().equalsIgnoreCase(password)){
            // 1. 更新当前用户的登录时间
            Date createTime = new Date(System.currentTimeMillis());
            user.setLoginTime(createTime);
            userDao.save(user);

            // 2. 重新生成新的token令牌
            String tokenContent = email + password;
            UUID uuid = UUID.nameUUIDFromBytes(tokenContent.getBytes());

            LagouToken token = new LagouToken();
            token.setEmail(email);
            token.setToken(uuid.toString());
            return tokenDao.save(token);
        }else {
            return null;
        }

    }

    @Override
    public boolean isRegistered(String email) {

        LagouUser user = userDao.findFirstByEmail(email);
        if (user != null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String getEmailByToken(String token) {

        LagouToken lagouToken = tokenDao.findFirstByToken(token);
        return token == null ? "not found" : lagouToken.getEmail();
    }
}
